﻿@model ThreadEditModel

@{
    ViewData["Title"] = new string[] { Model.ThreadId > 0 ? "编辑贴子" : "发布贴子" };
    var user = UserContext.CurrentUser;
    var Section = ViewData.Get<Section>("Section", new Section());
    var selectListItems = ViewData.Get<List<SelectListItem>>("SelectListItems", new List<SelectListItem>());
    var threadcategories = ViewData.Get<IEnumerable<Category>>("threadcategories", null);
    var attachmentList = ViewData.Get<IEnumerable<Attachment>>("attachmentList", null);

}

@Styles.Render("~/Bundle/Styles/Uploader")


<div class="container tn-survey-form">
    <div class="jh-post-top tn-mb-20 jh-top-center">
        <div class="row">
            <div class="col-xs-1">
                <img src="@(new AttachmentService(TenantTypeIds.Instance().Section())?.Get(Section.FeaturedImageAttachmentId)?.GetDirectlyUrl("Small"))" width="60" alt="...">
            </div>
            <div class="jh-post-title col-xs-11">
                <div class="jh-title-left">
                    <h4><a href="@CachedUrlHelper.Action("BarSectionDetail", "Post", null, new RouteValueDictionary { { "sectionId", Section.SectionId } })"><strong>@Section.Name</strong></a></h4>
                </div>
                <div class="jh-title-right">
                    <ul class="list-inline">
                        <li>主题数：@Section.ThreadCount</li>
                        <li>回贴数：@(Section.ThreadAndPostCount - Section.ThreadCount)</li>
                        <li>用户数：@Section.GetFavoritedUserCount()</li>
                        @if (Section.Category != null)
                        {
                            <li>所属分类： <a href="@CachedUrlHelper.Action("BarSection", "Post", null, new RouteValueDictionary { { "categoryId", Section.Category.CategoryId } })">@Section.Category.CategoryName</a></li>
                        }
                    </ul>
                </div>
            </div>
        </div>
    </div>
    <div class="jh-padding-white">
        <h5 class="tn-mb-20">
            @if (Model.ThreadId == 0)
            {
                <text>发布贴子</text>
            }
            else
            {
                <text>编辑贴子</text>
            }
        </h5>
        @using (Html.BeginAjaxForm("EditThread", "Post", "", FormMethod.Post, new AjaxFormOptions { OnSuccessCallBack = "editThreadCallBack" }, new { @id = "EditThreadForm", @class = "form-horizontal tn-form-validation" }))
        {
            @Html.HiddenFor(n => n.ThreadId)
            @Html.HiddenFor(n => n.SectionId)
            @Html.HiddenFor(n => n.AssociateId)
            <div class="form-group">
                <div class="col-xs-12">
                    @Html.TextBoxFor(n => n.Subject, new { @class = "form-control", placeholder = "贴子标题" })
                    @Html.ValidationMessageFor(n => n.Subject)
                </div>
            </div>

                    <div class="form-group ">
                        <div class="col-xs-12 editorDIV">
                            @Html.HtmlEditor("Body", TenantTypeIds.Instance().Thread(), Model.AssociateId, Model.Body, new Dictionary<string, object> { { "style", "height:400px;" }, { "maximumWords", 20000 } }, "allsattachment")
                            @Html.ValidationMessageFor(n => n.Body)
                            <div id="Attachments" class="tn-mt-10">
                                @if (attachmentList != null && attachmentList.Any())
                                {
                                    foreach (var item in attachmentList)
                                    {
                                        if (!item.IsShowInAttachmentList)
                                        {
                                            continue;
                                        }
                                        <div class="tn-file-attachment tn-mb-10 text-left">
                                            <i class="fa fa-file-o"></i>
                                            @item.FriendlyFileName
                                            <a href="javascript:;" data-id="@item.AttachmentId" id="delete-@item.AttachmentId" class="pull-right remove-file">删除</a>
                                        </div>
                                    }
                                }
                            </div>
                        </div>

                    </div>
            if (threadcategories != null && threadcategories.Any())
            {
                <div class="form-group">
                    <label class="col-xs-1 control-label">贴子分类</label>
                    <div class="col-xs-3">
                        <select class="form-control" name="CategoryId">
                            @foreach (var item in threadcategories)
                            {
                                <option value="@item.CategoryId" @if (item.CategoryId == Model.CategoryId) { <text> selected</text>}>@item.CategoryName</option>
                            }
                        </select>
                    </div>
                </div>
            }
            <div class="form-group">
                <div class="col-xs-1">

                </div>
                <div class="col-xs-2">
                    <button class="btn btn-primary btn-submit" type="button">确定</button>
                    <button type="button" class="btn btn-default cancel">取消</button>
                </div>
            </div>
        }
    </div>
</div>

<script type="text/javascript">
    require(['jquery', 'ZeroClipboard', 'tnlayer', 'ueditor'], function ($, ZeroClipboard, tnlayer) {
        window['ZeroClipboard'] = ZeroClipboard;
        //提交
        $('.btn-submit').on("click", function () {
            $('#EditThreadForm').submit();
        })
        //删除附件
        $(document).on('click', 'a[id^=delete-]', function (e) {
            var $this = $(this);
            //添加时间戳
            var getTimestamp = new Date().getTime();
            var id = $(this).attr("id").replace(/[^0-9]/ig, "");
            $.post('@CachedUrlHelper.Action("_DeleteAttachments", "Common")?attachmentId=' + id + '&timestamp=' + getTimestamp, function myfunction(data) {
                if (data.MessageType) {
                    layer.msg(data.MessageContent, {
                        icon: 1
                    });
                    $this.parent().remove();
                }
                else {
                    layer.msg(data.MessageContent, {
                        icon: 2
                    });
                }
            });
        });
        //回调
        $.fn.editThreadCallBack = function (data) {
            if (data.state == 1) {
                layer.msg("操作成功", {
                    icon: 1
                });
                setTimeout(function () {
                    window.location.href = "@CachedUrlHelper.Action("ThreadDetail", "Post")?threadId=" + data.threadId;
                }, 1500);
            }
            else {
                layer.msg("发贴失败", {
                    icon: 2
                });
            }
        }

        function formIsDirty(form) {
            for (var i = 0; i < form.elements.length; i++) {
                var element = form.elements[i];
                var type = element.type;
                if (type == "text" || type == "textarea") {
                    if (element.value != element.defaultValue) {
                        return true;
                    }
                }
            }
            return false;
        }

        //取消
        $('.cancel').on("click", function () {
            if (formIsDirty(document.forms["EditThreadForm"])) {
                layer.confirm("编辑的内容还未提交，是否返回？", { btn: ['确认', '取消'] }, function () {
                    history.go(-1);
                })
            } else {
                history.go(-1);
            }
        })
    });

</script>
